package com.box.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.DbLog;
import com.box.entity.DbLogExample;
import com.box.entity.DbLogExample.Criteria;
import com.box.entity.DbOther;
import com.box.entity.DbOtherExample;
import com.box.entity.UserSearch;
import com.box.mapper.DbLogMapper;
import com.box.mapper.DbOtherMapper;
import com.box.service.LogService;
import com.box.utils.LayuiResult;
import com.box.utils.MyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class LogServiceImpl implements LogService {

	@Autowired
	private DbLogMapper logMapper;
	

	
	
	public LayuiResult insertLof(DbLog log) {
		// TODO Auto-generated method stub
		try {
			int n = logMapper.insert(log);
			return new LayuiResult(n, "没问题，添加成功");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "有问题");
		}

	}

	public LayuiResult selLogList(Integer page, Integer limit,UserSearch search) {
		// TODO Auto-generated method stub
		try {
			PageHelper.startPage(page, limit);
			DbLogExample example=new DbLogExample();
			example.setOrderByClause("create_time DESC");
			//创建条件查询
			Criteria createCriteria = example.createCriteria();
			if(search.getOperation()!=null && !"".equals(search.getOperation())){
				
				createCriteria.andOperationLike("%"+search.getOperation()+"%");
			}
			if(search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart())){
				createCriteria.andCreateTimeGreaterThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeStart()));
			}
			if(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd())){
				createCriteria.andCreateTimeLessThanOrEqualTo(MyUtil.getDateByString(search.getCreateTimeEnd()));
			}
			List<DbLog> logs = logMapper.selectByExample(example);
			PageInfo<DbLog> pageinfo=new PageInfo<DbLog>(logs);
			LayuiResult result=new LayuiResult();
			result.setCode(0);
			result.setCount(pageinfo.getTotal());
			result.setMsg("获取日志列表成功");
			result.setData(pageinfo.getList());
			return result;
		} catch (Exception e) {
			LayuiResult result=new LayuiResult();
			result.setCode(500);
			result.setCount(null);
			result.setMsg("获取日志列表失败");
			result.setData(e);
			return result;
		}

	}

	public Integer delLogsByDate(Date date) {
		// TODO Auto-generated method stub
	
			DbLogExample example = new DbLogExample();
			Criteria createCriteria = example.createCriteria();
			createCriteria.andCreateTimeLessThanOrEqualTo(date);
			int n = logMapper.deleteByExample(example);
			return n;
	
	
				

	}

	public List<DbLog> getLogList() {
		// TODO Auto-generated method stub
		DbLogExample example =new DbLogExample();
		example.setOrderByClause("create_time desc");
		
		
		return logMapper.selectByExample(example);
	}

	

}
